package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.entity.Goods;
import com.shop.shoporder.entity.order.ExportRecord;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.service.ExportRecordService;
import com.shop.shoporder.service.ExportService;
import com.shop.shoporder.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/export")
public class ExportController {
    @Autowired
    ExportService exportService;
    @Autowired
    ExportRecordService exportRecordService;
    @Autowired
    GoodsService goodsService;
    @ResponseBody
    @RequestMapping(value = "/exportExcel", method = RequestMethod.POST)
    public String exportExcel(HttpServletRequest request, HttpServletResponse response,@RequestBody JSONObject jsonParam) throws Exception {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        int AID=InterfaceVer.VerNumber( map.get("AID").toString());

       String res= exportService.exportExcel(request,response,AID);
       if("1".equals(res)){
           return "1";
       }else {
           return "0";
       }
    }

    @ResponseBody
    @RequestMapping(value = "/getRecordList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getRecordList(HttpServletRequest request, @RequestBody JSONObject jsonParam) throws Exception {
        int page =0;
        int limit =10;
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        String res = map.get("parm").toString();
        JSONObject param = JSONObject.parseObject(res);
        int Intlimit=InterfaceVer.VerNumber((String) param.get("limit"));
        int Intpage=InterfaceVer.VerNumber((String) param.get("page"));
        System.out.println("Intlimit:"+Intlimit+" Intpage:"+Intpage);
        if(Intpage>0){
        page=Intpage;
        limit=Intlimit;
        }
        PageHelper.startPage(page, limit);
        List<ExportRecord> list=exportRecordService.selectList();
        if(list!=null&&list.size()>0){
            PageInfo<ExportRecord> pageInfo=new PageInfo<>(list);
            int count=(int)pageInfo.getTotal();
            return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "获取成功", request);
        }else {
            return new Response(MessageConstants.MSG_SUCCESS, null, 0, "获取成功", request);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getUrl", method = RequestMethod.GET)
    public void getUrl(String path,HttpServletRequest request, HttpServletResponse response) throws Exception {
        exportService.doExport(path,request,response);
    }

    @ResponseBody
    @RequestMapping(value = "/readExcel", method = RequestMethod.POST)
    public String readExcel(MultipartFile file, String id,HttpServletRequest request, HttpServletResponse response) throws Exception {
       int ids= InterfaceVer.VerNumber(id);
//       ids=1;
        //System.out.println("sssss:"+1);
       if(ids>0){
           return exportService.readExcel(file,request,response);
       }else {
           return "登录失效";
       }

    }

    @ResponseBody
    @RequestMapping(value = "/selectSupplier", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response selectSupplier(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
//            String res = map.get("parm").toString();
//            int AID  = Integer.parseInt(map.get("AID").toString());
//            Goods goods1 = JSON.parseObject(res, new TypeReference<Goods>() {});
            List<Goods> listGoods = goodsService.selectSupplier();
            if (listGoods != null&&listGoods.size()>0) {
                return new Response(MessageConstants.MSG_SUCCESS, listGoods, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_SYS_ERROR, null, "没有数据", request);
            }
        }
    }

}
